﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using KL.UFrame.Api.Extensions;
using KL.UFrame.Core.Result;
using KL.UFrame.Data.Business;
using KL.UFrame.Data.Entities;
using KL.UFrame.Data.Models;
using Microsoft.AspNet.Identity;

namespace KL.UFrame.Api.ApiControllers
{
    [RoutePrefix("api/Venue")]
    public class VenueController : K9ApiBaseController
    {
        private IVenueBusiness _business;

        public VenueController(IVenueBusiness business)
        {
            _business = business;
        }

        [AllowAnonymous]
        [Route("BookedDays")]
        public async Task<ResultMessage<VenueBookModel>> GetBookedDays(int venueId, int month)
        {
            var result = await _business.GetVenueBookOfThisMonthAsync(venueId, month);
            return new ResultMessage<VenueBookModel>(result);
        }

        [AllowAnonymous]
        [Route("Book")]
        public async Task<ResultMessage> Book(VenueBookCreateModel model)
        {
            if (ModelState.IsValid == false) return ModelState.ToResultMessage();

            var accessToken = Request.Headers.GetCookies("_access_token");

            var userName = accessToken == null ? string.Empty : "admin";

            var result = await _business.BookAsync(model, userName);

            return result ? new ResultMessage(string.IsNullOrEmpty(userName) ?0:1) : new ResultMessage(-501, "保存数据失败,请稍后再试");
        }


        public async Task<ResultMessage<List<VenueModel>>> Get( bool onlyEnabled)
        {
            var model = await _business.GetAllVenuesAsync( onlyEnabled);
            return new ResultMessage<List<VenueModel>>(model);
        }

        public async Task<ResultMessage<VenueModel>> Get(int id)
        {
            var model = await _business.GetVenueAsync(id);
            return new ResultMessage<VenueModel>(model);
        }

        [HttpPost]
        [Route("Delete")]
        public async Task<ResultMessage<bool>> Delete(int id)
        {
            return new ResultMessage<bool>(await _business.DeleteVenueAsync(id));
        }

        [HttpPost]
        [Route("Resume")]
        public async Task<ResultMessage<bool>> Resume(int id)
        {
            return new ResultMessage<bool>(await _business.ResumeVenueAsync(id));
        }

        [Route("Update")]
        public async Task<ResultMessage> Update(VenueModel model)
        {
            ModelState.Remove("ImageUrl");
            if (!ModelState.IsValid)
            {
                return ModelState.ToResultMessage();
            }

            model.UpdateId = User.Identity.GetUserId();
            model.UpdateTime = DateTime.Now; ;
            var id = await _business.UpdateVenueAsync(model);
            return new ResultMessage(id);
        }

        [Route("Create")]
        public async Task<ResultMessage> Create(VenueModel model)
        {
            if (!ModelState.IsValid)
            {
                return ModelState.ToResultMessage();
            }

            model.CreateId = User.Identity.GetUserId();
            model.CreateTime = DateTime.Now; ;
            var id = await _business.CreateVenueAsync(model);
            return new ResultMessage(id);
        }

        [Route("BookRecord")]
        public async Task<ResultMessage<List<VenueBookRecordSearchResultModel>>> GetVenueBookRecordsAsync(int venueId,int month)
        {
            var year = month/100;
            var mon = month % 100;
            var start = new DateTime(year,mon,1);

            var result = await _business.GetVenueBookRecordsAsync(venueId, "", start, start.AddMonths(1));
            return new ResultMessage<List<VenueBookRecordSearchResultModel>>(result);
        }

       [Route("Audit")]
        public async Task<ResultMessage> Audit(int enrollId, string statusId, string description)
        {
            var status = VenueBookRecordStatus.New;

            if (!Enum.TryParse(statusId, out status))
            {
                return new ResultMessage(-1, "状态参数不正确");
            }

            if (status == VenueBookRecordStatus.Failed && string.IsNullOrEmpty(description))
            {
                return new ResultMessage(-2, "请输入不通过的原因");
            }
            var result = await _business.AuditAsync(enrollId, status, description);
            return result ? new ResultMessage() : new ResultMessage(-3, "审核失败，请稍后再试");
        }
    }
}
